package lector;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Entry;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import conector.ControladorConector;

public class ControladorLector {
	private CalendarService cs;
	
	private String user;
	private String password;
	
	private URL feedUrlQuery;
	private URL feedUrlCalendars;
	
	// Tiempo en long del tamano de un dia
	private static long DAYLENGTH = 86400000;
	
	public ControladorLector(String user, String password) {
		this.user = user;
		this.password = password;
		//this.cc = new ControladorConector(this);
		
		try {
			feedUrlQuery = new URL("https://www.google.com/calendar/feeds/default/private/full");
			feedUrlCalendars = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
		} catch (MalformedURLException mue) { }
	}
	
	public String[] getGoogleCalendars() {
		CalendarFeed resultFeed = null;
		// Por si se llama sin haber utilizado el metodo de conectar
		if (cs == null)
			if (!conectarGoogleCalendar())
				return null;
		try {
			resultFeed = cs.getFeed(feedUrlCalendars, CalendarFeed.class);
		} catch (ServiceException se) {
			se.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		String[] calendars = new String[resultFeed.getEntries().size()];
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		  CalendarEntry entry = resultFeed.getEntries().get(i);
		  calendars[i] = entry.getTitle().getPlainText();
		}
		return calendars;
	}
	
	public ArrayList<MiEvento> consultaGoogleCalendars() {
		CalendarQuery myQuery = new CalendarQuery(feedUrlQuery);
		ArrayList<MiEvento> myEvents = null;
		myQuery.setMinimumStartTime(DateTime.now());
		// Se permite tres semanas de prevision
		myQuery.setMaximumStartTime(new DateTime(DateTime.now().getValue() + 21 * DAYLENGTH));
		try {
			//Feed resultFeedQuery = cs.query(myQuery, Feed.class);
			CalendarEventFeed resultFeedQuery = cs.query(myQuery, CalendarEventFeed.class);
			myEvents = new ArrayList<MiEvento>();
			for (int i = resultFeedQuery.getEntries().size() - 1; i >= 0; i--) {
				CalendarEventEntry entry = resultFeedQuery.getEntries().get(i);
				MiEvento me = new MiEvento();
				// Informacion que se envia al conector
				me.setTitle(entry.getTitle().getPlainText());
				me.setLocation(entry.getLocations().get(0).getValueString());
				me.setDates(entry.getTimes().get(0).getStartTime(), entry.getTimes().get(0).getEndTime());
				myEvents.add(me);
				// System.out.println("Titulo: " + entry.getTitle().getPlainText());
				// System.out.println("Descripcion: " + entry.getPlainTextContent());
				// System.out.println("Lugar: " + entry.getLocations().get(0).getValueString());
				// System.out.println("Tiempo: " + entry.getTimes().get(0).getStartTime() + " - " + entry.getTimes().get(0).getEndTime());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return myEvents;
	}
	
	public boolean conectarGoogleCalendar() {
		boolean conecta = true;
		cs = new CalendarService("exampleCo-exampleApp-1");
		try {
			cs.setUserCredentials(user, password);
		} catch (AuthenticationException e) {
			System.out.println("Invalid credentials");
			conecta = false;
		}
		return conecta;
	}
	
	public String getUser() {
		return this.user;
	}
}